package com.vampmobile.android.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.vampmobile.R;
import com.vampmobile.android.VampCommon.Commons.KINDS;
import com.vampmobile.android.tools.ActivityParameters;
import com.vampmobile.android.tools.ApplicationMenu;

/*
 * class to display object and contact maps
 *  FO && AM
 */
public class SocialMap extends Activity {

	public static final String TAG = "Map";

	private ImageView mHomeTitle;
	private ImageView mHomeButton;
	private ImageView mBackButton;
	private ImageView mSearchButton;
	private ImageView mQuitButton;
	private ImageView mAccountButton;
	private ImageView mNavigation;
	private ImageView mGoogleMaps;
	private ImageView mVoiceSearch;
	private ImageView mAddress;
	private ImageView mPoiAddress;
	private ImageView mAddPoiAddress;
	private ProgressDialog mDialog;

	/*
	 * method used by clicking on the list of item
	 */
	class ItemClickListener implements OnClickListener {

		private String name = null;
		private Long id = null;
		private KINDS kind = null;

		public ItemClickListener(KINDS kind) {
			super();
			this.kind = kind;
			mDialog = new ProgressDialog(SocialMap.this);
		}

		// case video and music
		public ItemClickListener(KINDS kind, Long id, String name) {
			super();
			this.kind = kind;
			this.id = id;
			this.name = name;
		}

		public void onClick(View mView) {
			switch (kind) {
			case TOOLBAR:
				FrameLayout toolbar = (FrameLayout) findViewById(R.id.toolbar);
				if (toolbar.getVisibility() != View.VISIBLE)
					toolbar.setVisibility(View.VISIBLE);
				else
					toolbar.setVisibility(View.GONE);
				break;
			case QUIT:
				android.os.Process.killProcess(android.os.Process.myPid());
				java.lang.System.exit(0);
				finish();
				break;
			case SEARCH:
				break;
			case BACK:
				goHome();
				break;
			case PROFILE_TITLE:
				Intent myIntent = new Intent();
				myIntent.setClass(getApplicationContext(), Profile.class);
				myIntent.setAction(Intent.ACTION_VIEW);
				finish();
				startActivity(myIntent);
				break;
			case HOME_TITLE:
				Intent i = new Intent();
				i.setClass(getApplicationContext(), Home.class);
				i.setAction(Intent.ACTION_VIEW);
				finish();
				startActivity(i);
				break;
			case MAP_NAVIGATION:
				launchNavigation();
				break;
			case MAP_ADDRESS:
				launchAddress();
				break;
			case MAP_POI:
				lunchMapPOI();
				break;
			case MAP_ADD_ADR:
				goNew();
				break;
			// case MAP_VOICE_SEARCH:
			// break;
			case MAPS:
				launchMaps();
				break;
			}

		}
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setTitle(TAG);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.socialmap);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
		((TextView) findViewById(R.id.activityTitle)).setText("MAPS");

		// button of title bar
		FrameLayout toolbar = (FrameLayout) findViewById(R.id.toolbar);
		toolbar.setVisibility(View.GONE);
		mHomeTitle = (ImageView) findViewById(R.id.home_title);
		mHomeTitle.setOnClickListener(new ItemClickListener(KINDS.TOOLBAR));
		// button of toolbar
		mHomeButton = (ImageView) findViewById(R.id.home);
		mHomeButton.setOnClickListener(new ItemClickListener(KINDS.HOME_TITLE));
		mBackButton = (ImageView) findViewById(R.id.back_button);
		mBackButton.setOnClickListener(new ItemClickListener(KINDS.BACK));
		mSearchButton = (ImageView) findViewById(R.id.search);
		mSearchButton.setOnClickListener(new ItemClickListener(KINDS.SEARCH));
		mQuitButton = (ImageView) findViewById(R.id.quit);
		mQuitButton.setOnClickListener(new ItemClickListener(KINDS.QUIT));
		mAccountButton = (ImageView) findViewById(R.id.account);
		mAccountButton.setOnClickListener(new ItemClickListener(
				KINDS.PROFILE_TITLE));
		// lunch map navigation
		mNavigation = (ImageView) findViewById(R.id.navigation_img);
		mNavigation.setOnClickListener(new ItemClickListener(
				KINDS.MAP_NAVIGATION));
		// lunch google maps
		mGoogleMaps = (ImageView) findViewById(R.id.google_maps);
		mGoogleMaps.setOnClickListener(new ItemClickListener(KINDS.MAPS));
		// lunch map address
		mAddress = (ImageView) findViewById(R.id.address);
		mAddress.setOnClickListener(new ItemClickListener(KINDS.MAP_ADDRESS));
		// MAP add Address POI
		mAddPoiAddress = (ImageView) findViewById(R.id.maps_add_poi);
		mAddPoiAddress.setOnClickListener(new ItemClickListener(
				KINDS.MAP_ADD_ADR));
		// Map address POI
		mPoiAddress = (ImageView) findViewById(R.id.maps_poi);
		mPoiAddress.setOnClickListener(new ItemClickListener(KINDS.MAP_POI));
		// lunch command voice search
		mVoiceSearch = (ImageView) findViewById(R.id.voice_search);
		mVoiceSearch.setOnClickListener(new ItemClickListener(
				KINDS.MAP_VOICE_SEARCH));
	}

	void goToAdress(String logitude, String latitude) {
		Intent i = new Intent(Intent.ACTION_VIEW,
				Uri.parse("google.navigation:q=" + logitude + "," + latitude));
		startActivity(i);
	}

	void goToAdress(String adress) {
		Intent i = new Intent(Intent.ACTION_VIEW,
				Uri.parse("google.navigation:q=" + adress));
		startActivity(i);
	}

	void launchAddress() {

		Intent intent = new Intent("android.intent.action.MAIN");

		intent.setComponent(new ComponentName("com.google.android.apps.maps",
				"com.google.android.maps.PlacesActivity"));
		startActivity(intent);
	}

	void launchMaps() {
		Intent intent = new Intent("android.intent.action.VIEW");
		intent.setData(Uri.parse("google.navigation:fd=true"));
		intent.setComponent(new ComponentName("com.google.android.apps.maps",
				"com.google.android.maps.driveabout.app.NavigationActivity"));
		startActivity(intent);
	}

	void lunchMapPOI() {
		try {
			mDialog.setMessage("Loading POI Address ...");
			mDialog.setIndeterminate(true);
			mDialog.setCancelable(true);
			mDialog.show();
			Intent j = new Intent();
			j.setClass(getApplicationContext(), SocialMapPOI.class);
			j.setAction(Intent.ACTION_VIEW);
			finish();
			startActivity(j);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			mDialog.dismiss();
		}
	}

	void launchNavigation() {
		Intent intent = new Intent("android.intent.action.MAIN");

		intent.setComponent(new ComponentName("com.google.android.apps.maps",
				"com.google.android.maps.driveabout.app.DestinationActivity"));
		startActivity(intent);
	}

	public void goNew() {
		ActivityParameters.themetId = null;
		ActivityParameters.themeName = null;
		ActivityParameters.poiId = null;
		Intent i = new Intent();
		i.setClass(getApplicationContext(), PoiNew.class);
		i.setAction(Intent.ACTION_VIEW);
		finish();
		startActivity(i);
	}

	@Override
	public void onResume() {
		// clear message dialog
		if (mDialog != null) {
			mDialog.dismiss();
		}
		super.onResume();
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		ApplicationMenu.onCreateOptionsMenu(this, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		ApplicationMenu.onOptionsItemSelected(this, item);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		ApplicationMenu.onPrepareOptionsMenu(this, menu);
		return super.onPrepareOptionsMenu(menu);
	}

	public void goHome() {
		Intent i = new Intent();
		i.setClass(getApplicationContext(), Home.class);
		i.setAction(Intent.ACTION_VIEW);
		finish();
		startActivity(i);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// clear message dialog
		if (mDialog != null) {
			mDialog.dismiss();
		}
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			goHome();
			return true;
		}
		return false;
	}

	@Override
	public void finish() {
		// clear message dialog
		if (mDialog != null) {
			mDialog.dismiss();
		}
		super.finish();
	}

}